package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.dao.BrandDao;
import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.no.utils.PageUtils;
import com.atguigu.common.no.utils.Query;

import com.atguigu.gulimall.product.dao.CategoryBrandRelationDao;
import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {


    @Autowired
    private BrandDao brandDao;
    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private CategoryBrandRelationDao categoryBrandRelationDao;

    @Autowired
    private BrandService brandService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetile(CategoryBrandRelationEntity categoryBrandRelation) {
        Long id = categoryBrandRelation.getId();
        Long catalogid = categoryBrandRelation.getCatelogId();
     BrandEntity brandEntity= brandDao.selectById(id);
       CategoryEntity categoryEntity=categoryDao.selectById(catalogid);
        categoryBrandRelation.setBrandId(brandEntity.getBrandId());
        categoryBrandRelation.setCatelogId(categoryEntity.getCatId());
        this.save(categoryBrandRelation);
    }

    @Override
    public List<BrandEntity> getByBrabdsBYid(Long catId) {
      List<CategoryBrandRelationEntity> categoryBrandRelationEntities= categoryBrandRelationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catId));

      List<BrandEntity> brandEntityList=  categoryBrandRelationEntities.stream().map(item -> {
            Long id = item.getBrandId();
         BrandEntity brandEntity=   brandService.getById(id);
            return brandEntity;
        }).collect(Collectors.toList());

      List<BrandEntity> brandEntities = null;

        return brandEntityList;
    }
}